/**
 * 加算
 * @param num1
 * @param num2
 * @returns
 */
function accAdd(arg1, arg2) {
	var r1, r2, m, c,n;
	try {
		r1 = arg1.toString().split(".")[1].length;
	} catch (e) {
		r1 = 0;
	}
	try {
		r2 = arg2.toString().split(".")[1].length;
	} catch (e) {
		r2 = 0;
	}
	c = Math.abs(r1 - r2);
	m = Math.pow(10, Math.max(r1, r2));
	//获取数值的最大浮点位数，设置为返回值的精度长度
	n = Math.max(r1, r2)
	if (c > 0) {
		var cm = Math.pow(10, c);
		if (r1 > r2) {
			arg1 = Number(arg1.toString().replace(".", ""));
			arg2 = Number(arg2.toString().replace(".", "")) * cm;
		} else {
			arg1 = Number(arg1.toString().replace(".", "")) * cm;
			arg2 = Number(arg2.toString().replace(".", ""));
		}
	} else {
		arg1 = Number(arg1.toString().replace(".", ""));
		arg2 = Number(arg2.toString().replace(".", ""));
	}
	return ((arg1 + arg2) / m).toFixed(n);
}

/**
 * 精确计算两数的差
 * @method subtraction
 * @param {Number} v1 有效数值1
 * @param {Number} v2 有效数值2
 * @return {Number} v1-v2两数值的差
 */
function accSub(v1, v2) {
	var n1, n2, m, n
	//获取两数的浮点位数
	try {
		n1 = v1.toString().split('.')[1].length
	} catch (e) {
		n1 = 0
	}
	try {
		n2 = v2.toString().split('.')[1].length
	} catch (e) {
		n2 = 0
	}
	//获取10为底数，数值的最大浮点位数次幂
	m = Math.pow(10, Math.max(n1, n2))
	//获取数值的最大浮点位数，设置为返回值的精度长度
	n = Math.max(n1, n2)
	//toFixed格式化数值位数
	return ((v1 * m - v2 * m) / m).toFixed(n)
}

/**
 * 乗算
 *
 * @param arg1
 * @param arg2
 * @returns
 */
function accMul(arg1, arg2) {
	var m = 0,
		s1 = arg1.toString(),
		s2 = arg2.toString();
	try {
		m += s1.split(".")[1].length;
	} catch (e) {}
	try {
		m += s2.split(".")[1].length;
	} catch (e) {}
	return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);
}


export default {
	add: accAdd,
	sub: accSub,
	mul: accMul
}
